package com.example.eeuse.mysql.controller;







import java.util.*;

import java.util.stream.Collectors;
import java.util.stream.Stream;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.eeuse.mysql.mapper.UserMapper;


import com.example.eeuse.mysql.model.User;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TestUserController {
    private static final Logger logger = Logger.getLogger(TestUserController.class);

    @Autowired
    private UserMapper userMapper;



    /**
    *数据查询
    * */

    @GetMapping("/testSelectList")
    public void testSelectList(){

        //通过条件查询构造器查询一个list集合，若没有条件，可设置null作为参数

        /**根据id查询 select * from user where id=?
        userMapper.selectById(4L);
        logger.info(userMapper.selectById(4L));*/

        System.out.println("111");

        System.out.println("------------------------");


        /**查询 user表里面所有的信息  select * from user
        List<User> userList = userMapper.selectList(null);

        userList.forEach(System.out::println);*/



        /**
        * 根据多个id，查询多个用户信息
        *
        *
        *       List<Long> list = Arrays.asList(3L,4L);

        List<User> users = userMapper.selectBatchIds(list);
        users.forEach(System.out::println);
        * */






        /** 根据map中的条件，查询用户信息
        *
        * select * from user where name = ? and age = ?
        *
        * */


//        Map<String,Object> map = new HashMap<>();
//        map.put("name","xiaohai");
//        map.put("age",37);
//        List<User> users = userMapper.selectByMap(map);
//        users.forEach(System.out::println);


        Map<String, Object> stringObjectMap = userMapper.selectMapById(1694585361647620097L);
        System.out.println("stringObjectMap = " + stringObjectMap);


    }



    /**
    * 数据新增
    *
    * */

    @GetMapping("/testUserInsert")

    public void testUserInsert(){
        User user = new User();
        user.setName("zhangsan");
        user.setAge(19);
        user.setEmail("1257834076@qq.com");
        int result = userMapper.insert(user);
        System.out.println("result = " + result);
    }





    /**
    *删除方法
    * */

    @GetMapping("/testUserDelete")

    public void testUserDelete(){
        /* 根据id删除用户信息 id是用雪花算法生成的
        int i = userMapper.deleteById(1693440797935153154L);
        System.out.println("i = " + i);*/

       /*
       *
       *根据map集合设置的条件删除用户信息
       * */

       /*Map<String,Object> map = new HashMap<>();
       map.put("name","张三");
       map.put("age",24);
       int result = userMapper.deleteByMap(map);

       System.out.println("result = " + result);*/



       /**
       *批量根据id 删除 通过多个id实现批量删除 delete from user id in (?,?,?);
       * */


        List<Long> list = Arrays.asList(1693440797935153154L, 1693441807873228802L);
        int result = userMapper.deleteBatchIds(list);
        System.out.println("result = " + result);

    }





/**
* 更新方法
*
* */

    @GetMapping("/testUserUpdate")
    public void testUserUpdate(){


        User user =new User();
        user.setId(4L);
        user.setEmail("lisi@agtcom.com");
        user.setName("李四");
        int result = userMapper.updateById(user);
        System.out.println("result = " + result);
    }






    /**
     *
     * 查询总记录数
     *
     *
     * */
     @GetMapping("getCount")
    public void getCount(){
         User user = new User();
         Long id = user.getId();
         Long count = userMapper.getCount(id);
         System.out.println("count = " + count);
//         System.out.println("count = " + count);
     }


    /**
     *分页查询
     *
     */


    @GetMapping("/testSelectPageByDto")
    public void testSelectPageByDto() {
        //pageHelper 分页插件
        /*PageHelper.startPage(1, 4);
        List<User> list = userMapper.selectList(null);

//用PageInfo对结果进行包装
        PageInfo page = new PageInfo(list);
        System.out.println("page = " + page);*/



    }


    /**
     * 批量id删除
     *
     */

    @GetMapping("/batchId")

     public void batchId(){

        List<Long> ids = Stream.of(1693440797935153154L,1693441807873228802L).collect(Collectors.toList());

        int i = userMapper.deleteBatchIds(ids);
        System.out.println("i = " + i);

    }









    /**
     *
     * 批量添加操作
     *
     *
     * */

    @GetMapping("/getAdd")
    public void getAdd(){
        for(int i=0;i<9;i++){
            User user = new User();
            user.setName("zhanb"+i);
            user.setAge(20+i);
            user.setEmail("zhangb@qq.com");
            int userd = userMapper.insert(user);
            System.out.println("insert的结果 = " +userd);
        }


    }





    /**
     *
     * 根据id 批量查询
     *
     *
     * */

    @GetMapping("/selectBatch")

    public void selectBatch(){

        List<Long> ids = new ArrayList<>();

        ids.add(1694585361685368834L);
        ids.add(1694585361693757441L);


        List<User> users = userMapper.selectBatchIds(ids);

        System.out.println("users.size() = " + users.size());


        for (User u:users) {
            System.out.println("查询的用户: " + u);
        }
    }








    /**
     *
     *
     * 使用lambda 表达式批量查询id
     *
     * */


    @GetMapping("/lambdaSelect")


    public void lambdaSelect(){


        List<Long> collect = Stream.of(1694585361685368834L,1694585361693757441L, 1694585361681174530L).collect(Collectors.toList());


        List<User> users = userMapper.selectBatchIds(collect);

        //遍历集合

        users.forEach(u -> System.out.println("查询的user对象:" + u));

    }









}

